from model.uml_element import UmlElement
from model.uml_constraint import UmlConstraint
from model.uml_parameter import UmlParameter
from model.uml_xmi_info import UmlXmiInfo


class UmlOperation(UmlElement):
    def __init__(self) -> None:
        super().__init__(UmlXmiInfo.type_operation)
        self.name: str = ""
        self.isQuery: str = ""
        self.isAbstract: str = ""
        self.precondition: str = ""
        self.redefinedOperation: str = ""
        self.bodyCondition: str = ""
        self.ownedRules: list[UmlConstraint] = []
        self.ownedParameters: list[UmlParameter] = []
        # Transform 中使用的变量
        self.locked = False

    def to_json(self) -> dict[str, object]:
        json: dict = super().to_json()
        json["name"] = self.name
        json["isQuery"] = self.isQuery
        json["isAbstract"] = self.isAbstract
        json["precondition"] = self.precondition
        json["redefinedOperation"] = self.redefinedOperation
        json["bodyCondition"] = self.bodyCondition

        if self.ownedComment is not None:
            json["ownedComment"] = self.ownedComment.to_json()

        item = []
        for value in self.ownedRules:
            item.append(value.to_json())
        json["ownedRules"] = item
        
        item = []
        for value in self.ownedParameters:
            item.append(value.to_json())
        json["ownedParameters"] = item

        return json
